
using CStock;
using System;
using System.Diagnostics;
using CUtil;
using CTrading;
using TestCases.StockData;

using NUnit.Framework;
using CGrid;
using CIndicators;

namespace TestCases.TradeSimulations
{
	

	/// <summary>Some simple Tests.</summary>
	/// 
	[TestFixture] 
	public class TradeSimulationTest : DataClassTest
	{
		
        
		GridBase gridTrade;
        

        //ITradeSystemFactory tradeSystemFactory;

		[SetUp] 
		public void Init() 
		{
			gridTrade = new GridBase();
            DataItemFactory dif = new DataItemFactory();
            //dif.Create_TestData();

            Assert.IsNotNull(programContext.TradeEconomy.TradeSimulation.TradeSystem);
            
           CreateShareFromExcelFile();
			
            	
			
		}
        private IDataItem CreateShareFromExcelFile()
        {
            if (!this.programContext.IsInitialized)
                this.programContext.ProgramContext_Init(false);
            //have to see if there is a portfolio there and a dataItem, DataClass etc.

            //just brute force:

            this.programContext.Open_FromExcelFile(@"C:\Simulate.xls");

          
            IDataItem di = this.programContext.TradeEconomy.Portfolio_Simulation.DataItems[0];
            this.programContext.TradeEconomy.Portfolio_Simulation.DataItem_Selected = di;

            IDataClass dc = di.DataClass;
            Debug.WriteLine("Share from Excel File is: " + dc.Name);

            //IDataItem di = new DataItem();
            //IDataClass dc = excelReader.CreateDataClassAndReadValues("SiemensDummy");
            //di.DataClass = dc;
            //this.programContext.Trader_Simulation.Portfolio_Simulation.DataItem_Selected = di;

            Assert.IsNotNull(this.programContext.Trader_Simulation.Portfolio_Simulation);
            Assert.IsNotNull(this.programContext.Trader_Simulation.Portfolio_Simulation.DataItem_Selected);
            Assert.IsNotNull(this.programContext.Trader_Simulation.Portfolio_Simulation.DataItem_Selected.DataClass);
            Assert.IsNotNull(this.programContext.Trader_Simulation.Portfolio_Simulation.DataItem_Selected.DataClass.QuoteCollection);
            Assert.IsNotNull(programContext.TradeEconomy.TradeSimulation.TradeSystem);


            return di;


        }
      
        public void SimulateShare(IDataClass dc)
        {
            this.programContext.TradeEconomy.Portfolio_Simulation.DataItem_Selected.DataClass = dc;


            this.programContext.TradeEconomy.TradeSimulation.SimulationShare(this.programContext.TradeEconomy.Portfolio_Simulation.DataItem_Selected, TradeWriteModes.EndResultShareInFile, true, false, null);
			

        }
        [Test]
        public void AddOneDay()
        {
            ITradeEconomy tradeEconomy = this.programContext.TradeEconomy;

            //tradeEconomyFactory.DeSerialize();
            Assert.IsTrue(tradeEconomy.AddOneDay());
        }
		
        [Test]
        public void Portfolio_Simulate()
        {

            IPortfolio port = this.CreatePortfolio_Test();

            programContext.TradeEconomy.TradeSimulation.SimulationPortfolio_PerDate(this.programContext.TradeEconomy.Portfolio_Simulation, TradeWriteModes.OnlyTrade, false, null);

            
        }
        private IPortfolio CreatePortfolio_Test()
        {
            IPortfolioFactory portFactory = new PortfolioFactory();
            portFactory.Create("TestPortfolio");
            portFactory.Create_TestData();

            IPortfolio port = portFactory.StockObjectBase as IPortfolio;
            return port;

        }

        [Test] 
        public void Portfolio_SingleTradeSteps_100()
        {
          

            IPortfolio port = CreatePortfolio_Test();

            TradeStepFactory.StepPortfolio(port, null, false);

            //programContext.TradeEconomy.TradeSimulation.StepPortfolio();

            
        }
        [Test]
        public void Portfolio_Optimize()
        {

            IPortfolio port = CreatePortfolio_Test();
            

            //dataClass.TradeSimulation.Optimize(false);
            programContext.TradeEconomy.TradeSimulation.Optimize( port, null, false, null);
            ITradeStep ts = port.TradeStepPortfolio;

            Debug.WriteLine("Simulation done, Performance is " + ts.Performance.ToString() + "; BuyHold is : " + ts.PerformanceBuyAndHold_Share.ToString());
            Debug.WriteLine("         ---Trades: " + ts.TradesNumber);

        }
        [Test] 
        public void Share_SingleTradeSteps_100()
        {
            IDataItem myDataItem = programContext.TradeEconomy.Portfolio_Simulation.DataItem_Selected;

            ITradeStep ts = programContext.TradeEconomy.Portfolio_Simulation.DataItem_Selected.TradeStep;
            TradeStepFactory.FirstInit_TradeStep(myDataItem, programContext.TradeEconomy.TradeSimulation.TradeSystem);

            
            ts.TradeWriteMode = TradeWriteModes.EachTradeStep;
            for (int i = 1; i < 100; i++)
            {
                Debug.WriteLine(ts.Date.ToShortDateString() + " : Step done, quote:  " + ts.QuoteShare + " ;Indicator value : " + ts.IndicatorValue);
                if (ts.TradeType != CUtil.TradeType.Nothing)
                {
                    Debug.WriteLine(" --- " + ts.TradeType.ToString() + " ---  occured " + ts.QuoteShare + " ts." + ts.MaxMoneyForBuyPerShare);

                    
                }


                TradeStepFactory.StepShareIncreaseDate(myDataItem, myDataItem.TradeStep.TradeSystem, false, null, false);
            }

          
            return;
        }
         [Test]
        public void Share_Simulate()
        {
            
            Share_Simulate_Old_RandomData();


         }
        [Test]
        public void Share_Simulate_Old_RandomData()
        {
            IDataItem myDataItem = programContext.TradeEconomy.Portfolio_Simulation.DataItem_Selected;

            ITradeStep ts = myDataItem.TradeStep;

            TradeStepFactory.FirstInit_TradeStep(myDataItem, programContext.TradeEconomy.TradeSimulation.TradeSystem);

            
            ts.TradeWriteMode = TradeWriteModes.EachTradeStep;

            programContext.TradeEconomy.TradeSimulation.SimulationShare(myDataItem, TradeWriteModes.Nothing, false, false, null);

            Debug.WriteLine("Simulation done, Performance is " + ts.Performance.ToString() + "; BuyHold is : " + ts.PerformanceBuyAndHold_Share.ToString());
            Debug.WriteLine("         ---Trades: " + ts.TradesNumber);
    

           


            return;
        }
       
        [Test]
        public void Share_Optimize()
        {
            IDataItem myDataItem = programContext.TradeEconomy.Portfolio_Simulation.DataItem_Selected;

            ITradeStep ts = myDataItem.TradeStep;
            ts.TradeWriteMode = TradeWriteModes.EachTradeStep;

            programContext.TradeEconomy.TradeSimulation.Optimize(null, myDataItem, true, null);

            Debug.WriteLine("Simulation done, Performance is " + ts.Performance.ToString() + "; BuyHold is : " + ts.PerformanceBuyAndHold_Share.ToString());
            Debug.WriteLine("         ---Trades: " + ts.TradesNumber);
            
     
        }


		
	}
	
}